package com.mmc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mmc.entity.UserBind;
import com.mmc.entity.UserInfo;
import com.mmc.exception.Assert;
import com.mmc.hfb.FormHelper;
import com.mmc.hfb.HfbConst;
import com.mmc.hfb.RequestHelper;
import com.mmc.mapper.UserBindMapper;
import com.mmc.result.ResponseEnum;
import com.mmc.result.UserBindEnum;
import com.mmc.service.IUserBindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mmc.service.IUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-20
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements IUserBindService {
    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public String authBind(UserBind userBindUpload, Long userId) {
        //判断身份证是否已经绑定过了
        LambdaQueryWrapper<UserBind> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBind::getIdCard, userBindUpload.getIdCard())
                .ne(UserBind::getId, userId);
        UserBind one = this.getOne(queryWrapper);

        Assert.isNull(one, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);
        //判断是否正在绑定
        queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBind::getUserId,userId);
        UserBind userBind = this.baseMapper.selectOne(queryWrapper);

        if (userBind==null){
            userBind=new UserBind();
            BeanUtils.copyProperties(userBindUpload,userBind);
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
            userBind.setUserId(userId);
            this.baseMapper.insert(userBind);
        }else {
            userBind.setUserId(userId);
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
            this.updateById(userBind);
        }
        //前往汇付宝认证
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("agentId", HfbConst.AGENT_ID);
        paramMap.put("agentUserId",userBind.getUserId());
        paramMap.put("idCard",userBind.getIdCard());
        paramMap.put("personalName",userBind.getName());
        paramMap.put("bankType",userBind.getBankType());
        paramMap.put("bankNo",userBind.getBankNo());
        paramMap.put("mobile",userBind.getMobile());
        paramMap.put("returnUrl",HfbConst.USERBIND_RETURN_URL);
        paramMap.put("notifyUrl",HfbConst.USERBIND_NOTIFY_URL);
        paramMap.put("timestamp", RequestHelper.getTimestamp());
        paramMap.put("sign",RequestHelper.getSign(paramMap));
        String formStr = FormHelper.buildForm(HfbConst.USERBIND_URL, paramMap);

        return formStr;
    }

    @Override
    public void mmcNotify(Map<String, Object> map) {
        String userId = (String) map.get("agentUserId");
        if (StrUtil.isNotBlank(userId)){
            UserBind userBind = this.getOne(new LambdaQueryWrapper<UserBind>().eq(UserBind::getUserId, userId));
            //修改绑定状态
            userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
            //设置绑定码
            userBind.setBindCode((String) map.get("bindCode"));
            this.updateById(userBind);

            //修改UserInfo表
            UserInfo userInfo = userInfoService.getById(userId);
            userInfo.setName(userBind.getName());
            userInfo.setIdCard(userBind.getIdCard());
            userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
            userInfo.setBindCode((String) map.get("bindCode"));
            userInfoService.updateById(userInfo);


        }
    }
}
